<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

namespace app\common\model;

use think\Model;
use think\Db;
class Department extends Model
{
    /**
     * 获得部门列表
     */
    public function getList($admin_reg){
        
		$parent_id = Db::name('plaform')->where(['admin_reg'=>$_SESSION["think"]["reg"]])->field('parentid')->find();
		if($parent_id['parentid']){

			$adreg = Db::name('plaform')->where(['id'=>$parent_id['parentid'],'status'=>1])->field('admin_reg')->find();
			$rg = empty($adreg['admin_reg']) ? [$admin_reg] : [$admin_reg,$adreg['admin_reg']];
			
			$arr=$this->field('id,name,pid,company_id')->where(['status'=>1,'admin_reg'=>['in',$rg]])->select();
			
		} else {
			  $arr=$this->field('id,name,pid,company_id')->where('status=1 and admin_reg= "'.$admin_reg.'" ')->select();
			
		}
		
		
        return $arr;
    }
    /**
     * 通过id获得部门
     */
    public function getListById($id){
        $arr=$this->field('id,name,pid')->where('id='.$id)->select();
        return $arr;
    }
    public function getListByCompanyId($id,$admin_reg){
        $arr=$this->field('id,name,pid')->where('status=1 and admin_reg= "'.$admin_reg.'" and company_id='.$id)->select();
        return $arr;
    }
    /**
     * 通过部门id获取部门下属分组
     * @author sunman
     */
    public function getGroupList($pid){
        $arr=$this->field('id,name,pid,company_id')->where('status=1 and pid="'.$pid.'" and isdelete=0')->select();
        return $arr;   
    }
	
	/**
     * 通过id获取部门对应的用户
     * @author sunman
     */
    public function getGroupListUserid($pid,$child_plaform,$marks){
		if($marks=='D'){ //部门
			$maps['departmentid'] = $pid;
		} else { //班组
			$maps['departmentgroupid'] = $pid;
		}

		$maps['admin_reg'] = ['in',$child_plaform];

        $arrsc = Db::name('user_company')->field('id')->where($maps)->select();
		$identity_id = ['identity_id'=>[]];
		foreach($arrsc as $v){
			$identity_id['identity_id'][] = $v['id'];
		}

        return $identity_id;
		
        return $arr;
    }
	
}